package com.hw.blog.service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hw.blog.persistence.PmgrBoardMapper;


@Service
public class PmgrBoardService {

	@Autowired
	public PmgrBoardMapper mapper;
	

	public List brdList(Map vo) throws Exception {
		return mapper.brdList(vo);
	}
	public int brdListCnt(Map vo) throws Exception {
		return mapper.brdListCnt(vo);
	}
	
	
	public Map brdEdit(Map vo) throws Exception {
		return mapper.brdEdit(vo);
	}
	
	public List brdFileEdit(Map vo) throws Exception {
		return mapper.brdFileEdit(vo);
	}

	
	public int getBdNo(Map vo) throws Exception {
		return mapper.getBdNo(vo);
	}
	
	public int updateBrd(Map vo) throws Exception {
//		int bd_no = service.getBdNo(vo);
//		vo.put("bd_no", bd_no);
//		service.insertBrd(vo);
		if(null != vo.get("bd_no") && !"".equals(vo.get("bd_no") )){
//			수정
			mapper.updateBrd(vo);
		}else{
			//신규
//			고유번호 가져오기
			int bd_no = mapper.getBdNo(vo);
			vo.put("bd_no", bd_no);
			mapper.insertBrd(vo);
		}
		if(null != vo.get("bdf_no")){
			fileProcess(vo);	
		}

		return mapper.updateBrd(vo);
	}
	
	/**
	 * 파일 처리
	 * @param vo
	 */
	public void fileProcess(Map vo){
		String[] bdf_no = (String[]) vo.get("bdf_no");
		String[] upload = (String[]) vo.get("upload");
		String[] upload_nm = (String[]) vo.get("upload_nm");
		String[] upload_sz = (String[]) vo.get("upload_sz");
		String[] del_yns = (String[]) vo.get("del_yns");
		String wasPath = vo.get("was_path").toString();
		for(int i= 0;i< bdf_no.length;i++){
			if("".equals(bdf_no[i].trim())){
				//신규추가 파일
				if("Y".equals(del_yns[i])){
					//파일 삭제
					deleteFile(wasPath+upload[i]);
				}else{
					//신규파일 등록
					Map dataMap = new HashMap();
					dataMap.put("bd_no", vo.get("bd_no"));
					dataMap.put("menu_seq", vo.get("menu_seq"));
					dataMap.put("p_file_nm", upload[i]);
					dataMap.put("view_file_nm", upload_nm[i]);
					dataMap.put("file_sz", upload_sz[i]);
					mapper.insertBrdFile(dataMap);
				}
			}else{
				if("Y".equals(del_yns[i])){
					//파일 삭제
					deleteFile(wasPath+upload[i]);
					//디비삭제
					Map dataMap = new HashMap();
					dataMap.put("bd_no", vo.get("bd_no"));
					dataMap.put("bdf_no", bdf_no[i]);
					dataMap.put("menu_seq", vo.get("menu_seq"));
					
					mapper.deleteBrdFile(dataMap);
				}
			}
		}
	}
	public void deleteFile(String filePath){
		File del = new File(filePath);
		if(del.exists()){
			del.delete();
		}
	}
	
	public int deleteProcess(Map vo) throws Exception {
//		return mapper.deleteBrd(vo);
		return 0;
	}

	public void insertCmt(Map vo) throws Exception {
		mapper.insertCmt(vo);
//		return 0;
	}
	

	public Map getTopCmt(Map vo) throws Exception {
		return mapper.getTopCmt(vo);
//		return 0;
	}

	

}
